// 单例模式实现思路1：实例挂载到 class 上
class Singleton1 {
  constructor(name) {
    this.name = name
  }

  static getInstance(name = '') {
    if (!Singleton1.instance) {
      Singleton1.instance = new Singleton1(name)
    }
    return Singleton1.instance
  }
}

const s1 = Singleton1.getInstance()
const s2 = Singleton1.getInstance()
console.log(s1, s2, s1 === s2)

// 单例模式实现思路2：闭包
class Singleton2 {
  constructor(name) {
    this.name = name
  }
}
Singleton2.getInstance = (function () {
  let instance = null
  return function (name = '') {
    if (!instance) {
      instance = new Singleton2(name)
    }
    return instance
  }
})()

const s3 = Singleton2.getInstance()
const s4 = Singleton2.getInstance()
console.log(s3, s4, s3 === s4)
